True
and False
; not true nor TRUEa = True; b = false
. How to fix it?
True = "Is it okay?"
. How to fix it?
==
equal to!=
not equal to>
greater than>=
greater than or equal to<
less than<=
less than or equal toage = 21 age == 40 age < 40 age <= 41 age > 40 age >= 40
not
and
or
age = 21 age <= 16 or age > 35 # What value? (age > 16) and (age <= 35) # ??? not age > 35 # ???
if condition: statement or statements # There should be an indentation before a statement or indentaitons before statements.
age = 21 if age < 35: print("Young") print(age)
row = ???(input("Enter the row number: ") # Integer 0, 1, or 2 is expected. if row < 0 ??? row > 2: print("Wrong row number") else print("Correct row number")
age = 40 if age < 35: print("Young") print(age) else: print("Not young anymore")
print("Young"); print(age)
belong to if age < 35:
.
print(age)
belong to if age < 35:
?age = 40 if age < 35: print("Young") print(age)
age = input("Age: ") if age >= 20: if age >= 60: print("Well aged"); else: print("Adult") else: print("Teenager, or") print("Kid")
elif
can used for else:\n if
. Let's try the next example. What will be printed? Anything wrong? How to fix it?age = input("Age: ") name = ???("Name: ") if name == "John" and age == 20: print("John Doe") else: if name == 'John' ??? age == 25: print("John Gilbert") else: print("Who are you?") if name == "John" and age == 20: print("John Doe") elif name == 'John' ??? age == 25: print("John Gilbert") else??? print("Who are you?")
Repeat 20 times the followings: Read a number Add the number to a variable Divide the variable by 20 Print the result
count = 0 Repeat the followings while count < 20: Read a number Add the number to a variable Increase count by 1 Divide the variable by 20 Print the result
count = 0 Repeat the followings while count < 20: number = Read a number sum = sum + number count = count + 1 Divide the variable by 20 Print the result
while condition: ...
count = ??? sum = ??? while count < 20: number = input("Enter a number: ") sum = sum + number count = count + 1 average = sum / 20 print("Average = " + average);
While the user wants to keep playing, repeat the followings: Print the value; Increase the value;
While answer is 'yes', repeat the followings: Print the value Increase the value answer = input("Continue? ")
???? while answer == 'yes': print(value) value = value + 1 answer = input("Continue? ")
Repeat the followings forever: # forever? how? If the user enters a negative value: # how? break the loop # how? Add the value into sum # how? Print the sum
???? while True: # called infinite loop value = input("Enter a value: ") If value < 0: break # the break statement to exit out of a loop sum = sum + value print("The sum is " + sum)
Repeat the followings forever: # how? password <- Read a password phrase If the length of password < 5: Skip to the next password # how? Print password count = count + 1 if count >= 5: Stop the loop # how?
???? while True: password = input("Enter a password: ") if len(password) < 5: print("Too short password phrase") continue # the continue statement to the next iteration of a loop print(password) count = count + 1 if count >= 5: break # the break statement to exit out of a loop
???? # infinite loop row = int(input("Enter row number: ")) if ???? ???? ???? # infinite loop column = int(input("Enter column number: ")) if ???? ???? print(row) print(column)
for i in range(10): print(i)
i
will have 0, 1, ..., 9.
Repeat the followings with i from 1 upto 1000: sum = sum + i Print sum
for i in range(1001): # 0, 1, 2, ..., 1000
for i in range(1001): sum = sum + i print(sum)
'|'
symbols.
E.g., |Wonterful world!|
.strww = 'What a wonderful world!' print('|', end='') for c in strww: # for each character in strww print(c, end='') # end='' makes the next character be printed in the same line. print('|') # another idea using string concatenation prtstr = '' prtstr = prtstr + '|' # string concatenation for c in strww: # for each character in strww prtstr = prtstr + c prtstr = prtstr + '|' print(prtstr)
range()
range()
can have multiple arguments that are separated by commas.range(10, 20)
. What values are printed?
range()
can have three arguments.
range(end) # 0, ..., end-1 range(start, end) # start, start+1, ..., end-1 range(start, end, step) # start, start+step, ...
range(10) # 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 range(3, 10) # 3, 4, 5, 6, 7, 8, 9 range(0, 10, 2) # 0, 2, 4, 6, 8
range()
can be used for negative numbers as well. See the next example.
for i in range(10, -5, -2): # 10, 8, 6, 4, 2, 0, -2, -4 print(i)
???? for i in range(????): ???? print(sum)
input()
, print()
, int()
, float()
, len()
, range()
, ...
They are difined in a standard library that is automatically included (oac imported) in our programs.
All the Python programs can use them.
import random for i in range(5): r = random.randint(0, 19) # .randint() - a random integer in [0, 19] (or [0, 20)) print(r)
import random for random in range(5): r = random.randint(10, 100-1) print(r)
import sys, os, random
from random import * for i in range(5): r = randint(-10, 10) # randint() is defined in random. print(r) for i in range(5): r = random() # random() generates a random floats in [0, 1). print(r) for i in range(5): r = uniform(-5, 2.5) # uniform(a, b) generates a random floats in [a, b). print(r)
exit()
.
E.g.,
import random for i in range(1000): r = random.randint(0, 20-1) if r == 0 or r == 1: exit() print(r)
random.choice()
'|'
.
E.g., |M|1|0|0|1|M|2|M|1|0|
for "M1001M2M10"
.